package org.fuys.coder.application.admin;

import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.entity.Result;
import org.fuys.coder.common.holder.UserHolder;
import org.fuys.coder.domain.act.activity.model.req.ActivityAddReq;
import org.fuys.coder.domain.act.lottery.model.req.award.AwardSelectReq;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyDetailVO;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyVO;
import org.fuys.coder.domain.auth.model.annotation.PermissionCertify;
import org.fuys.coder.domain.auth.model.vo.permission.PermissionTypeVO;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.domain.act.lottery.model.req.act.LotteryActivityAddReq;
import org.fuys.coder.domain.act.lottery.model.req.award.AwardAddReq;
import org.fuys.coder.domain.act.lottery.model.req.strategy.StrategyAddReq;
import org.fuys.coder.domain.act.lottery.model.vo.award.AwardVO;
import org.fuys.coder.domain.act.lottery.service.activity.ILotteryActivityService;
import org.fuys.coder.domain.act.lottery.service.award.ILotteryAwardService;
import org.fuys.coder.domain.act.lottery.service.strategy.ILotteryStrategyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application
 * @className: ActivityAdminController
 * @author: WangJing
 * @description: 抽奖管理页面控制器 包括增删改查
 * @date: 2024/7/8 11:03
 * @version: 1.0
 */
@RestController
@RequestMapping("fuyscoder/admin/act")
public class ActivityAdminController {

    @Resource
    private ILotteryStrategyService lotteryStrategyService;

    @Resource
    private ILotteryActivityService lotteryActivityService;

    @Resource
    private ILotteryAwardService lotteryAwardService;

    @PermissionCertify(permission = PermissionTypeVO.ACTIVITY_MODIFY)
    @PostMapping("/lottery/add")
    public Result addLottery(@RequestBody LotteryActivityAddReq req){
        try{
            lotteryActivityService.addActivity(req);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    @PermissionCertify(permission = PermissionTypeVO.ACTIVITY_MODIFY)
    @PostMapping("/lottery/strategy/add")
    public Result addStrategy(@RequestBody StrategyAddReq req){
        try {
            lotteryStrategyService.addStrategy(req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    @PermissionCertify(permission = PermissionTypeVO.ACTIVITY_SELECT)
    @PostMapping("/lottery/strategy/page")
    public Result getPageStrategy(PageDTO pageDTO){
        List<Pair<LotteryStrategyVO, LotteryStrategyDetailVO>> resultList=null;
        try {
            resultList=lotteryStrategyService.getPageStrategyInfo(pageDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok().data(resultList);
    }

    @PermissionCertify(permission = PermissionTypeVO.ACTIVITY_MODIFY)
    @PostMapping("/add")
    public Result addActivity(@RequestBody ActivityAddReq req){

        return Result.ok();
    }
    @PermissionCertify(permission = PermissionTypeVO.AWARD_MODIFY)
    @PostMapping("/award/add")
    public Result addAward(@RequestBody AwardAddReq req){
        try{
            final Integer userId = UserHolder.get();
            lotteryAwardService.addAward(req,userId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    @PermissionCertify(permission = PermissionTypeVO.AWARD_SELECT)
    @PostMapping("/award/page")
    public Result getPageAward(@RequestBody AwardSelectReq req){
        List<AwardVO> list=null;
        try{
            list=lotteryAwardService.getPageAward(req);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.ok().data(list);
    }
}
